// pages/info/info.js
const t = getApp()

Page({

  /**
   * 页面的初始数据
   */
  data: {
    iconsUrl:t.globalData.iconsUrl,
    shopname:'',
    shops:false,
    shoparr: [ // value传给后台的值 ， label页面上显示的文字
          {value:'0', label:'全部门店'},
        ],
    loading:true,
    user_id:'',
    id:'',
    b_id:0,
    accountIndex:0,
    select1:1,
    select2:1,
    select3:2,
    box1show:false,
    showbox3:false,
    intime:'--',
    outtime:'--',
    change:0,
    keyword:'',
    page1:1,
    page2:1,
    page3:1,
    total1:0,
    total2:0,
    total3:0,
    canpre1:false,
    canpre2:false,
    canpre3:false,
    cannext1:true,
    cannext2:true,
    cannext3:true,
    listData1:[],
    listData2:[],
    defaultImg:'/images/bce5.jpg',
    users:[],
    userinfo:[],
    showbox5:false,
    showbox4:false,
    add:'',
    take:'',
    currentIndex: 0,
    height1:0,
    switch1Checked:false, //拉黑
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.cklogin();
    this.setData({
      id:parseInt(options.id)
    })
  },
//id:parseInt(options.id)
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.get_shops();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onRefresh(){
    //在当前页面显示导航条加载动画
    wx.showNavigationBarLoading(); 
    //显示 loading 提示框。需主动调用 wx.hideLoading 才能关闭提示框
    that.get_shops();
  },
  onPullDownRefresh: function () {
    //调用刷新时将执行的方法
    this.onRefresh();
    setTimeout(function () {
      // 不加这个方法真机下拉会一直处于刷新状态，无法复位
      wx.stopPullDownRefresh()
    }, 1000)
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },



  cklogin(){
    let user_id=wx.getStorageSync('user_id');
    if(user_id){
      let user=wx.getStorageSync('user');
      this.setData({
        user_id:user_id,
        u_name: user.u_name
       })
    }else{
      wx.navigateTo({
        url: '/pages/login/login',
      })
    }
  },
  get_shops(){
    var that = this;
    if(wx.getStorageSync('user_id')){
      t.request('/Merchant/MerchantSide/storeList',{user_id:that.data.user_id},function(e){
        let list=e.result.list;
        that.setData({
          accountIndex:t.globalData.accountIndex,
        })
        if(list.length>1){//多店铺
          let gid=t.globalData.b_id;
          if(gid!=0){
            that.setData({
              b_id:gid,
            })
          }
          let shoparr=[{value:'0', label:'全部门店'},];
          for(let i=0;i<list.length;i++){
            let arr ={};
            arr['value']=list[i].store_id;
            arr['label']=list[i].title;
            shoparr.push(arr);
          }
          console.log(shoparr);
          that.setData({
            shops:true,
            shoparr:shoparr
          })
        }else{
          that.setData({
            shopname:e.result.list[0].title,
            b_id:e.result.list[0].store_id,
          })
        }
        that.get_cards();
        that.get_cars();
        that.get_recharge();
      })
      
    }
  },
  get_cards(){
    var that = this;
    let id=that.data.id;
    if(!id){
      id='';
    }
    t.request('/Merchant/MerchantSide/cardsInfo',{user_id:that.data.user_id,shop_id:that.data.b_id,id:id},function(e){
      if(e.result.data.length!=0){
        that.setData({
          userinfo:e.result.data[0],
          switch1Checked:e.result.data[0].blacklist==0?false:true,
        })
        console.log(e.result.data[0]);
      }else{
        let userinfo=that.data.userinfo;
        userinfo.balance=0;
        userinfo.card_id='';
        userinfo.create_time='';
        userinfo.prize_pay=0;
        userinfo.real_pay=0;
        userinfo.store_id='';
        userinfo.total=0;
        that.setData({
          userinfo:userinfo,
          switch1Checked:false,
        })
      }
      
    })
  },
  get_cars(){
    var that = this;
    let id=that.data.id;
    if(!id){
      id='';
    }
    t.request('/Merchant/MerchantSide/carsInfo',{user_id:that.data.user_id,shop_id:that.data.b_id,id:id,page1:that.data.page1},function(e){
      that.setData({
        listData1:e.result.data,
        total1:e.result.total_page
      })
    })
  },
  get_recharge(){
    var that = this;
    let id=that.data.id;
    if(!id){
      id='';
    }
    t.request('/Merchant/MerchantSide/rechargeInfo',{user_id:that.data.user_id,shop_id:that.data.b_id,id:id,page1:that.data.page2},function(e){
      let height1=e.result.data.length;
      that.setData({
        listData2:e.result.data,
        total2:e.result.total_page,
        height1:height1
      })
    })
  },
  input1(e){
    this.setData({
      id:e.detail.value,
    })
  },
  input2(e){
    this.setData({
      keyword:e.detail.value,
    })
  },
  input3(e){
    this.setData({
      add:e.detail.value,
    })
  },
  // 拉黑用户
  switch1Change(e){
    console.log("拉黑");
    let that=this;
    t.request('/Merchant/MerchantSide/blackList',{card_id:that.data.userinfo.card_id,blacklist:e.detail.value},function(e){

    })
  },
  input4(e){
    this.setData({
      take:e.detail.value,
    })
  },
  gopre1(){
    let page1=this.data.page1;
    console.log(page1);
    if(page1<=1 || page1 -1==1){
      this.setData({
        canpre1:false,
      })
    }
    if(page1-1==0){
      this.setData({
        page1:1,
        canpre1:false
      })
    }else{
      this.setData({
        page1:page1-1,
        cannext1:true
      })
      this.get_cars();
    }
  },
  gonext1(){
    let page1=this.data.page1;
    if(page1+1==this.data.total1){
      this.setData({
        cannext1:false,
      })
    }
    if(page1+1>this.data.total1){
    }else{
      this.setData({
        page1:page1+1,
        canpre1:true
      })
      this.get_cars();
    }
  },
  gopre2(){
    let page2=this.data.page2;
    if(page2<=1){
      this.setData({
        canpre2:false,
      })
    }
    if(page2<=1 || page2 -1==0){
      console.log(1);
    }else{
      this.setData({
        page2:page2-1,
        cannext2:true
      })
      this.get_recharge();
    }
  },
  gonext2(){
    let page2=this.data.page2;
    if(page2+1==this.data.total2){
      this.setData({
        cannext2:false,
      })
    }
    if(page2+1>this.data.total2){
    }else{
      this.setData({
        page2:page2+1,
        canpre2:true
      })
      this.get_recharge();
    }
  },
  gopre3(){
    let page3=this.data.page3;
    if(page3<=1){
      this.setData({
        canpre3:false,
      })
    }
    if(page3<=1 || page3 -1==0){
      console.log(1);
    }else{
      this.setData({
        page3:page3-1,
        cannext3:true
      })
      this.do_keyword_seach();
    }
  },
  gonext3(){
    let page3=this.data.page3;
    if(page3+1==this.data.total3){
      this.setData({
        cannext3:false,
      })
    }
    if(page3+1>this.data.total3){
    }else{
      this.setData({
        page3:page3+1,
        canpre3:true
      })
      this.do_keyword_seach();
    }
  },
  do_keyword_seach(){
    let that=this;
    t.request('/Merchant/MerchantSide/searchUser',{user_id:that.data.user_id,shop_id:that.data.b_id,keyword:that.data.keyword,page1:that.data.page3},function(e){
      console.log(e.result);
      that.setData({
        users:e.result.info,
        total3:e.result.total_page
      })
    })
  },
  do_add(){
    let that=this;
    let id=that.data.now_id;
    console.log(id);
    let b_id=that.data.b_id;
    if(!id || b_id==0){
      that.setData({
        showbox4:false
      })
      wx.showToast({
        title: '请选择门店和用户',
      })
    }else{
      t.request('/Merchant/MerchantSide/balanceChange',{user_id:that.data.now_id,shop_id:that.data.b_id,nums:that.data.add,type:1},function(e){
        console.log(e.result);
        that.setData({
          showbox4:false
        })
        wx.showToast({
          title: '赠费成功！',
        })
        that.get_shops();
      })
    }
  },
  do_take(){
    let that=this;
    let id=that.data.now_id;
    if(!id){
      that.setData({
        showbox5:false
      })
      wx.showToast({
        title: '请选择门店和用户',
      })
    }else{
      t.request('/Merchant/MerchantSide/balanceChange',{user_id:that.data.now_id,shop_id:that.data.b_id,nums:that.data.take,type:2,id:id},function(e){
        console.log(e.result);
        that.setData({
          showbox5:false
        })
        wx.showToast({
          title: '扣费成功！',
        })
        that.get_shops();
      })
    }
    
  },

  do_showbox3(){
    this.setData({
      showbox3:true,
      showbox1:false,
      showbox2:false,
      showbox4:false,
      showbox5:false

    })
  },
  do_showbox4(e){
    this.setData({
      now_id:e.currentTarget.dataset.id,
      showbox4:true,
      showbox3:false,
      showbox1:false,
      showbox2:false,
      showbox5:false
    })
  },
  do_showbox5(e){
    this.setData({
      now_id:e.currentTarget.dataset.id,
      showbox5:true,
      showbox4:false,
      showbox1:false,
      showbox2:false,
      showbox3:false
    })
  },
  set_id(e){
    this.setData({
      id:e.currentTarget.dataset.id,
      showbox3:false,
      users:[]
    })
    this.get_cards();
    this.get_cars();
    this.get_recharge();
    this.onLoad();
  },
  do_nav(e){
    let url= '/pages/record/record?car_id=""&user_id='+e.currentTarget.dataset.id
    wx.navigateTo({
      url,
    })
  },

  onRefresh(){
    //在当前页面显示导航条加载动画
    wx.showNavigationBarLoading(); 
    //显示 loading 提示框。需主动调用 wx.hideLoading 才能关闭提示框
    this.get_shops();
  },
  onPullDownRefresh: function () {
    //调用刷新时将执行的方法
    this.onRefresh();
    setTimeout(function () {
      // 不加这个方法真机下拉会一直处于刷新状态，无法复位
      wx.stopPullDownRefresh()
    }, 1000)
  },
  
  navigate(e){
    let page=e.currentTarget.dataset.page
    if (page == 1) {
      wx.redirectTo({
        url: '/pages/index/index',
      })
    }else if (page == 2) {
      wx.redirectTo({
        url: '/pages/info/info',
      })
    }else if(page == 3){
      wx.redirectTo({
        url: '/pages/record/record',
      })
    }else if(page == 4){
      wx.redirectTo({
        url: '/pages/shop/shop',
      })
    }else if(page == 5){
      wx.navigateTo({
        url: '/pages/info/info',
      })
    }
  },
  bindAccountChange(e){
    console.log('切换改变的值', e.detail.value);
    let bid=this.data.shoparr[e.detail.value];
    console.log(bid.value);
    t.globalData.b_id=bid.value;
    t.globalData.accountIndex=e.detail.value;
    this.setData({
      b_id: bid.value,
      accountIndex:e.detail.value
    })
    this.get_shops();
  },
  showbox1(e){
    this.setData({
      showbox1: true
    })
    setTimeout(()=>
    {
      this.setData({
        showbox1: false
      })
    }, 2000)
  },
  loginout(e){
    this.setData({
      showbox1: false
    })
    wx.removeStorageSync("user");
    wx.removeStorageSync("user_id");
    wx.navigateTo({
      url: '/pages/login/login',
    })
  },
  close2(e){
    console.log(1);
    this.setData({
      showbox3: false
    })
  },
  close4(e){
    this.setData({
      showbox4: false
    })
  },
  close5(e){
    this.setData({
      showbox5: false
    })
  },
  pagechange: function (e) {
    // 通过touch判断，改变tab的下标值
    if ("touch" === e.detail.source) {
      let currentPageIndex = this.data.currentIndex;
      currentPageIndex = (currentPageIndex + 1) % 2;
      // 拿到当前索引并动态改变
      this.setData({
        currentIndex: currentPageIndex,
      })
    }
  },

  //点击tab时触发
  titleClick: function (e) {
    this.setData({
      //拿到当前索引并动态改变
      currentIndex: e.currentTarget.dataset.idx
    })
  },
})